package com.tank.action;

import java.util.List;

import javax.print.attribute.standard.MediaSize.Other;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.tank.bean.User;
import com.tank.service.BuddyService;
import com.tank.util.Pager;
import com.tank.util.TankUtil;
import com.tank.vo.Buddy;

@Controller("buddyAction")
@Scope("prototype")
public class BuddyAction extends ActionSupport {
	@Autowired
	@Qualifier("buddyService")
	BuddyService service;
	User master;
	User target;
	List<Buddy> buddies;
	String type;
	

	Pager pager;
	
	boolean success;
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}
	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public User getMaster() {
		return master;
	}

	public void setMaster(User master) {
		this.master = master;
	}

	public User getTarget() {
		return target;
	}

	public void setTarget(User target) {
		this.target = target;
	}

	public List<Buddy> getBuddies() {
		return buddies;
	}

	public void setBuddies(List<Buddy> buddies) {
		this.buddies = buddies;
	}

	public String fans() throws Exception {
		this.setType("fans");
		if(master == null) {
			master=TankUtil.getCurrentUser();
			pager=service.findFansList(master);
			return SUCCESS;
		}else{
			master=TankUtil.getUser(master.getId());
			pager=service.findOthersFansList(master);
			return SUCCESS;
		}
	
		
		
	}

	public String follows() throws Exception {
		
		this.setType("follows");
		if(master == null) {
			master=TankUtil.getCurrentUser();
			pager=service.findFollowsList(master);
			return SUCCESS;
		}else{
			master=TankUtil.getUser(master.getId());
			pager=service.findOthersFollowsList(master);
			return SUCCESS;
		}
	}
	
	public String find() throws Exception {
		this.setType("find");
		System.out.println("find:"+master.getName());
		
			pager=service.findBuddyList(master);
			return SUCCESS;
		
	}

	public String add() throws Exception {
		if(master == null) master=TankUtil.getCurrentUser();
		this.setType("add");
		success=true;
		if(service.add(master,target)){
			return SUCCESS;
		}
		
		return SUCCESS;
	}
	
	public String remove() throws Exception {
		if(master == null) master=TankUtil.getCurrentUser();
		this.setType("remove");
		success=true;
		if(service.remove(master,target)){
			return SUCCESS;
		}
		
		return SUCCESS;
	}

}
